package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConLine;
import com.htgd.gardenexpo.entity.PageQuery;
import com.htgd.gardenexpo.service.CommonService;
import com.htgd.gardenexpo.service.GeConLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 王秀田
 * @date: 2024/12/6 16:40
 * @description: TODO
 */
@RestController
@RequestMapping("/api/line")
@Api(tags = "线路景点配置管理")
public class GeConLineController {

    @Autowired
    private GeConLineService geConLineService;

    @Autowired
    private CommonService commonService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add(@RequestBody GeConLine geConLine){
        JSONArray jsonArray = JSONArray.parseArray(geConLine.getSceneIds());
        geConLine.setSceneIds(jsonArray.toJSONString());
        geConLine.setSceneNum(jsonArray.size());
        geConLine.setCreateTime(LocalDateTime.now());
        geConLineService.save(geConLine);
        return CommonResult.success("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult edit(@RequestBody GeConLine geConLine) {
        JSONArray jsonArray = JSONArray.parseArray(geConLine.getSceneIds());
        geConLine.setSceneIds(jsonArray.toJSONString());
        geConLine.setSceneNum(jsonArray.size());
        geConLineService.updateById(geConLine);
        return CommonResult.success("修改成功");
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(String id) throws Exception {
        geConLineService.removeById(id);
        return CommonResult.success("删除成功");
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConLine>> list(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<GeConLine> lineLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(pageQuery.getValue()!=null&&pageQuery.getValue()!=""){
            lineLambdaQueryWrapper.like(GeConLine::getTitle,pageQuery.getValue()).orderByDesc(GeConLine::getCreateTime);
        }
        List<GeConLine> list = geConLineService.list(lineLambdaQueryWrapper);
        list.stream().forEach(item->{
            if(item.getSceneIds()==null||item.getSceneIds()==""){
                item.setScenes(new ArrayList<>());
            }else {
                List<Integer> idList = JSONArray.parseArray(item.getSceneIds()).stream().mapToInt(value1 -> Integer.parseInt(value1.toString())).boxed().collect(Collectors.toList());
                item.setScenes(commonService.getSceneListById(idList));
            }
        });
        return CommonResult.success(String.valueOf(new PageInfo<>(list).getTotal()),new PageInfo<>(list).getList());
    }

}
